package com.example.planetandzombiedemo.global;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.DisplayMetrics;

/**
 * 处理设备适应性
 */
public class DeviceHelper {
    private static int[] deviceSize = new int[2];

    /**
     * 对位图进行缩放处理
     *
     * @param bitmap
     * @return 位图文件对象
     */
    public static Bitmap resizeBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            //进行变换，矩阵变换
            Matrix matrix = new Matrix();
            matrix.postScale(Config.scaleWidth, Config.scaleHeight);
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            return resizedBitmap;
        } else {
            return null;
        }
    }

    /**
     * 对位图进行指定大小的缩放处理
     *
     * @param bitmap
     * @param w
     * @param h
     * @return 位图文件对象
     */
    public static Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth = w;
            int newHeight = h;
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            //进行变换，矩阵变换
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            return resizedBitmap;
        } else {
            return null;
        }
    }

    /**
     * 获取设备尺寸
     *
     * @param context
     * @return
     */
    public static int[] getDeviceInfo(Context context) {
        if (deviceSize[0] == 0 && deviceSize[1] == 0) {
            DisplayMetrics metrics = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
            deviceSize[0] = metrics.widthPixels;
            deviceSize[1] = metrics.heightPixels;
        }
        return deviceSize;
    }
}
